package com.qfedu.lc.user.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.lc.common.param.UserVipAdd;
import com.qfedu.lc.common.vo.R;
import com.qfedu.lc.user.entity.Vip;
import com.qfedu.lc.user.entity.VipOrder;
import com.qfedu.lc.user.mapper.VipMapper;
import com.qfedu.lc.user.service.VipOrderService;
import com.qfedu.lc.user.service.VipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.lc.user.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 8.用户收费会员表 服务实现类
 * </p>
 *
 * @author KK
 * @since 2024-06-11
 */
@Service
public class VipServiceImpl extends ServiceImpl<VipMapper, Vip> implements VipService {

    @Autowired
    private VipOrderService vipOrderService;
    @Autowired
    private WalletService walletService;

    @Autowired
    private VipMapper vipMapper;

    @Override
    public R buyVipByWallet(UserVipAdd userVipAdd) {
        // 1,创建一个会员的订单
        VipOrder vipOrder = vipOrderService.createVipOrder(userVipAdd);
        // 2.扣减账户余额 记录日志
        walletService.subWallet(userVipAdd.getUserId(), userVipAdd.getMoney());
        // 3.修改会员订单状态
        vipOrder.setStatus(2);
        vipOrderService.updateById(vipOrder);
        //4.创建，购买 或者 续费 vip会员
        boolean result = buyOrUpdateVip(userVipAdd);
        //5.返回
        if (result) {
            return R.ok("vip购买成功");
        }
        return R.fail("vip购买成功");
    }

    //查询vip


    public boolean buyOrUpdateVip(UserVipAdd userVipAdd) {
        //1.查询当前用户是否有vip
        VipOrder vipOrder = new VipOrder();
        QueryWrapper<Vip> vipQueryWrapper = new QueryWrapper<>();
        vipQueryWrapper.eq("user_id", userVipAdd.getUserId());

        Vip vip = vipMapper.selectOne(vipQueryWrapper);
        // 没有vip 创建
        // 有vip   续费
        if (vip == null) {
            //没有vip创建
            vip = new Vip();
            vip.setUserId(userVipAdd.getUserId());
            vip.setInfo("购买vip");
            vip.setType(userVipAdd.getVipType());
            vip.setDays(365);
            vip.setCtime(new Date());

            vip.setSdate(new Date()); // 开始时间
            //结束时间
            Calendar calendar = Calendar.getInstance();//当前是时间
            calendar.add(Calendar.DAY_OF_MONTH, 365);
            Date eDate = calendar.getTime();
            vip.setEdate(eDate);
            int num = vipMapper.insert(vip);
            if (num > 0) {
                return true;
            }
        } else {
            vip.setInfo("续费vip");
            vip.setType(userVipAdd.getVipType());

            //1.vip已经过期了
            //2.vip还没有过期
            if (vip.getEdate().before(new Date())) {
                // vip已经过期了
                vip.setDays(365);
                vip.setSdate(new Date());
                Calendar calendar = Calendar.getInstance();//当前是时间
                calendar.add(Calendar.DAY_OF_MONTH, 365);
                Date eDate = calendar.getTime();//得到一年后的今天
                vip.setEdate((eDate));
            } else {
                // vip还没有过期
                Calendar calendar = Calendar.getInstance();//设置为原有会员卡的结束时间再加365
                calendar.setTime(vip.getEdate());//原来的过期时间
                calendar.add(Calendar.DAY_OF_MONTH, 365);
                Date eDate = calendar.getTime();//得到 再加一年后的今天
                long betweenDay = DateUtil.between(new Date(), vip.getEdate(), DateUnit.DAY);

                vip.setDays((int) (betweenDay + 365));
                vip.setEdate(eDate);
            }
            // 更新vip
            int num = vipMapper.updateById(vip);

            if (num > 0) {
                return true;
            }
        }
        return false;
    }


}
